package com.weimob.mcloud.wechatwork.customer.third;

import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.core.adapter.enums.channel.WechatWorkAppType;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.base.BaseAuthInputReqDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.base.BaseAuthReqDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.base.RateLimit4CustomerDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.constant.SuiteTypeEnum;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.request.ExternalUseridToPendingIdDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.request.SelfExternalContactGetDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.ExternalUseridToPendingIdRespDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.SelfExternalContactGetBO;
import com.weimob.mcloud.wechatwork.core.adapter.service.ChannelAdapterService;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.enums.privateSphere.PrivateSphereEncryptCorpEnum;
import com.weimob.mcloud.wechatwork.customer.mq.dto.CorpChatGroupDTO;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author chenliuzhu
 * @date 2023/1/13
 */
@Component
@Slf4j
public class ChannelAdapterServiceProxy {

    @Autowired
    private ChannelAdapterService channelAdapterService;

    public List<ExternalUseridToPendingIdRespDTO.Result> getPendingIdList(CorpChatGroupDTO groupDTO, PrivateSphere privateSphere, List<String> externalUserIds) {
        BaseAuthReqDTO<ExternalUseridToPendingIdDTO> baseAuthReq = new BaseAuthReqDTO<>();
        ExternalUseridToPendingIdDTO pendingIdRequest = new ExternalUseridToPendingIdDTO();
        BaseAuthInputReqDTO auth = new BaseAuthInputReqDTO();
        auth.setCorpid(groupDTO.getCorpId());
        if (StringUtils.isNotBlank(privateSphere.getTemplateId())) {
            auth.setType(SuiteTypeEnum.PROXY_ENTERPRISE_INTERNAL_APP.getCode());
            auth.setTemplateId(privateSphere.getTemplateId());
        } else {
            auth.setType(SuiteTypeEnum.ENTERPRISE_INTERNAL_ENTERPRISE.getCode());
        }
        baseAuthReq.setAuth(auth);
        pendingIdRequest.setChatId(groupDTO.getChatId());
        pendingIdRequest.setExternalUserid(externalUserIds);
        baseAuthReq.setRequest(pendingIdRequest);

        ExternalUseridToPendingIdRespDTO pendingIdResp = null;
        try {
            pendingIdResp = SoaUtil.unpack(channelAdapterService.externalUseridToPendingId(baseAuthReq));
        } catch (Exception e) {
            log.error("getPendingIdList 查询pendingId失败,externalUserIds:{} error: ", JSON.toJSON(externalUserIds), e);
        }
        if (Objects.isNull(pendingIdResp)) {
            return Collections.emptyList();
        }
        return pendingIdResp.getResult();
    }


    /**
     * 查询客户详情-接入限频
     *
     * @param privateSphere  私域
     * @param externalUserId 外部联系人id
     * @param rateLimitDTO   限流参数
     * @return
     */
    public SoaResponse<SelfExternalContactGetBO, Void> getSelfExternalContact(PrivateSphere privateSphere, String externalUserId, RateLimit4CustomerDTO rateLimitDTO) {
        SelfExternalContactGetDTO selfExternalContactGetDTO = new SelfExternalContactGetDTO();
        selfExternalContactGetDTO.setCorpId(privateSphere.getCorpId());
        selfExternalContactGetDTO.setExternalUserId(externalUserId);
        selfExternalContactGetDTO.setBizLimitKey(rateLimitDTO.getBizLimitKey());
        selfExternalContactGetDTO.setLimitKey(rateLimitDTO.getLimitKey());
        selfExternalContactGetDTO.setTaskId(rateLimitDTO.getTaskId());
        if (PrivateSphereEncryptCorpEnum.CIPHER.getCode().equals(privateSphere.getEncryptCorpId())) {
            selfExternalContactGetDTO.setTemplateId(privateSphere.getTemplateId());
            selfExternalContactGetDTO.setType(WechatWorkAppType.PROXIED_INTERNAL_APP.getType());

        } else {
            selfExternalContactGetDTO.setType(WechatWorkAppType.CONTACT.getType());
        }
        selfExternalContactGetDTO.setProviderIdentity(privateSphere.getChannelProviderIdentity());
        return channelAdapterService.getSelfExternalContact(selfExternalContactGetDTO);
    }
}
